package com.example.cart_system.controller;

import com.example.cart_system.pojo.*;
import com.example.cart_system.service.UsrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UsrController {
    @Autowired
    private UsrService usrService;

    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody Usr usr) {
        boolean f = usrService.register(usr);
        if (f) {
            return new Result<Boolean>().success("注册成功");
        }
        return new Result<Boolean>().error("用户已存在");
    }
    @PostMapping("/login")
    public Result<UsrInfo> login(@RequestBody Usr usr) {
        UsrInfo usrInfo = usrService.login(usr);
        if (usrInfo != null) {
            return new Result<UsrInfo>().success(usrInfo);
        }
        return new Result<UsrInfo>().error("用户名或密码错误");
    }
    @PostMapping("/buy")
    public Result<Boolean> buy(@RequestBody Order order) {
        usrService.buy(order);
        return new Result<Boolean>().success(true);
    }
    @PostMapping("/allName")
    public Result<List<Usr>> selectAllUsrName() {
        return new Result<List<Usr>>().success(usrService.selectAllUsrName());
    }
}
